package com.donar.concurrent.condition;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by donar on 18/3/1.
 */
public class ConditionDemo {
    ReentrantLock lock  = new ReentrantLock();
    Condition condition = lock.newCondition();

    public void run() throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                    System.out.println("wait signal");
                    condition.await();
                    System.out.println("get signal");
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }).start();
        Thread.sleep(1000);
        lock.lock();
        System.out.println("main --- lock ");
        System.out.println("signal");
        Thread.sleep(5000);
        condition.signalAll();
        System.out.println("main --- unlock ");
        lock.unlock();

    }

    public static void main(String[] args) throws InterruptedException {
        new ConditionDemo().run();
    }
}
